package com.oc.mba.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CommDateUtil {

	/**
	 * 取得当前日期的0点0分0秒
	 * @return 当前日期的0点0分0秒
	 */
	public static Timestamp today() {

		Timestamp ts;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			// 取得Timestamp类型的当前系统时间
			Timestamp today = new Timestamp(System.currentTimeMillis());
			String str = df.format(today);

			Date date11 = df.parse(str);

			String time = df.format(date11);

			ts = Timestamp.valueOf(time);
			return ts;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 取输入参数日期过去的多少天
	 * 
	 * @param beginDate 日期
	 * @param dayNum 天数
	 * @return
	 */
	public static Timestamp dateAdditionDay(Date beginDate, Integer dayNum) {

		Timestamp ts;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar date = Calendar.getInstance();
			date.setTime(beginDate);
			date.set(Calendar.DATE, date.get(Calendar.DATE) + dayNum);
			Date endDate = df.parse(df.format(date.getTime()));
			String time = df.format(endDate);
			ts = Timestamp.valueOf(time);
			return ts;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String DateToString(java.util.Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String datestr = null;
		try {
			datestr = formatter.format(date);
		}
		catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return datestr;
	}
}
